﻿
var Application = {
    type_offer_id: '',
    type_transport_id: '',
    type_offer:'',
    type_transport:'',
    company_id: ''
};

var Loading = {
    country_id:'',
    city: '',
    postcode: '',
    date: '',
    time: '',
    ampm:'',
    airport_id:'0',
    seaport_id:'0',
    other_airport: '',
    other_seaport: ''
};

var Unloading = {
    country_id: '',
    city: '',
    postcode: '',
    date: '',
    time: '',
    ampm: '',
    airport_id: '0',
    seaport_id: '0',
    other_airport:'',
    other_seaport:''
};

var Loading_Arr = new Array();
var Unloading_Arr = new Array();

var Step2CargoTransport = {
  loading_type_id: 1,
  step2_id: 1
}

var Step3Transport = {
    level_importance_id: '',
    vehicle_cat: '',
    universal_type_id:'',
    placement_type_id:'',
    packing_type_id:'',
    length:'',
    width:'',
    height:'',
    weight:'',
    volume:'',
    price:'',
    special_conditions_id:'',
    other_information:'',
    app_id:''
}

var Step3Air = {
    app_id : '',
	airport_loading_id : '',
	airport_unloading_id : '',
	level_importance_id : '',
	universal_type_id : '',
	placement_type_id : '',
	packing_type_id : '',
	length : '',
    width  : '',
    height : '',
    weight : '',
    volume : '',
    price : '',
    special_conditions_id : '',
    other_information : '',
    type: ''
}

var Step3SeaCargo = {
    app_id: '',
    seaport_loading_id: '',
    seaport_unloading_id: '',
    level_importance_id: '',
    universal_type_id: '',
    placement_type_id: '',
    packing_type_id: '',
    length: '',
    width: '',
    height: '',
    weight: '',
    volume: '',
    price: '',
    special_conditions_id: '',
    other_information: ''
}

var Step3SeaTransport = {
    level_importance_id: '',
    placement_type_id: '',
    country_id:'',
    seaport_id:'',
    universal_type_id:'',
    vessel_build_date:'',
    flag_country_id:'',
    clas:'',
    date_duration:'',
    time_duration :'',
    price:'',
    special_conditions_id:'',
    other_information:'',
    route:''
}

var Step3Tender = {
  opening_date:'',
  closing_date: '',
  transport_type_id:'',
  packing_type_id: '',
  placement_type_id: '',
  type_goods_id: '',
  weight_goods: '',
  payment_type_id: '',
  special_condition_type: '',
  other_information: ''
  
}

var loaderDiv = "<div id='loader' class='loading' align='center'></div>";


function CreateApplication() {

    $('#formDiv').modal({
        onShow: AppModalShow,
        autoResize: true,
        position: ["15%", "25%"],
        onClose:closeModal
    });
}

function LoadVehicleTypes() {
    $.ajax
        ({
            url: "webservices/Application.asmx/LoadVehicleTypes",
            data: '{cat_id:"' + $("#step3_road_ddl_vehicle_cat").val() + '"}',
            dataType: "json",
            async: false,
            contentType: "application/json; charset=utf-8",
            type: "POST",
            complete: function (json1) {
                var c = JSON.parse(json1.responseText);
                var d = JSON.parse(c.d);
                ddl_veh_type = $("#step3_road_ddlVehicleTypes");
                ddl_veh_type.html('');
                $('<option value="0"></option>').appendTo(ddl_veh_type);
                var record_count = 0;
                for (var i in d) {
                    record_count++;
                    $('<option value="' + i + '">' + d[i] + '</option>').appendTo(ddl_veh_type);
                }
                //$('<option value="">Other</option>').appendTo(ddl_to_add);

            }
        });
}

function ValidateAddMore(type) {

    var country = $('#step21_ddlCountry' + type);
    var city = $('#step21_tbCity' + type);
    var postcode = $('#step21_tbPostCode' + type);
    var date = $('#step21_tbDate' + type);
    var returnVal = true;

    country.css('border-style', 'solid 1px');
    country.css('border-color', '#000');
    $('#' + country.attr('id') + '_rfv').css('color', '#000');
    city.css('border-style', 'solid 1px');
    city.css('border-color', '#000');
    $('#' + city.attr('id') + '_rfv').css('color', '#000');
    postcode.css('border-style', 'solid 1px');
    postcode.css('border-color', '#000');
    $('#' + postcode.attr('id') + '_rfv').css('color', '#000');
    date.css('border-style', 'solid 1px');
    date.css('border-color', '#000');
    $('#' + date.attr('id') + '_rfv').css('color', '#000');
    
    if (country.val()=='0'){

        $('#' + country.attr('id') + '_rfv').css('color', 'red');
        country.css('border-style', 'solid 1px');
        country.css('border-color', 'red');
        returnVal = false;
    }
    if (city.val() == '') {

        $('#' + city.attr('id') + '_rfv').css('color', 'red');
        city.css('border-style', 'solid 1px');
        city.css('border-color', 'red');
        returnVal = false;
    }
    if (postcode.val() == '') {

        $('#' + postcode.attr('id') + '_rfv').css('color', 'red');
        postcode.css('border-style', 'solid 1px');
        postcode.css('border-color', 'red');
        returnVal = false;
    }
    if (date.val() == '') {
        $('#' + date.attr('id') + '_rfv').css('color', 'red');
        date.css('border-style', 'solid 1px');
        date.css('border-color', 'red');
        returnVal = false;
    }

    return returnVal;
}

function LoadingData(num) 
{
    for (var i = 1; i <= 4; i++) {
        $('#btn_loading_' + i).css('background-color', '#000');
        $('#btn_loading_' + i).attr('selected', 'false');
    }
    $('#btn_loading_' + (num + 1)).css('background-color', '#ccc');
    $('#btn_loading_' + (num + 1)).attr('selected', 'true');

    if (typeof (Loading_Arr[num]) != 'undefined') {
        $('#step21_ddlCountryLoading').val(Loading_Arr[num].country_id);
        $('#step21_ddlCountryLoading').change();
        $('#step21_tbCityLoading').val(Loading_Arr[num].city);
        $('#step21_tbPostCodeLoading').val(Loading_Arr[num].postcode);
        $('#step21_tbDateLoading').val(Loading_Arr[num].date);
        $('#step21_tbTimeLoading').val(Loading_Arr[num].time);
        $('#step21_ddlAMPMLoading').val(Loading_Arr[num].ampm);
        $('#step21_ddlAirportLoading').val(Loading_Arr[num].airport_id);
        $('#step21_ddlSeaPortLoading').val(Loading_Arr[num].seaport_id);

        //Other Aiport
        $('#AddOtherLoading').html('');
        if (typeof (Loading_Arr[num].other_airport) != 'undefined') {
            if (Loading_Arr[num].other_airport != '') 
            {
                var other_Airport = (Loading_Arr[num].other_airport).split(' - ');
                $('#step21_ddlAirportLoading').val('');
                LoadOtherStep2('', '', 'ddlAirportLoading', true, other_Airport[0], other_Airport[1]);
                $('#AddOtherLoading').show();
            }
        }
        
        //Other Seport
        if (typeof (Loading_Arr[num].other_seaport) != 'undefined') {
            if (Loading_Arr[num].other_seaport != '') 
            {
                var other_SeaPort = (Loading_Arr[num].other_seaport).split(' - ');
                $('#step21_ddlSeaPortLoading').val('');
                LoadOtherStep2('', '', 'ddlSeaPortLoading', true, other_SeaPort[0], other_SeaPort[1]);
                $('#AddOtherLoading').show();
            }
        }
    }
    else {
        $('#step21_ddlCountryLoading').val('0');
        $('#step21_tbCityLoading').val('');
        $('#step21_tbPostCodeLoading').val('');
        $('#step21_tbDateLoading').val('');
        $('#step21_tbTimeLoading').val('0');
        $('#step21_ddlAMPMLoading').val('0');
        $('#step21_ddlAirportLoading').val('0');
        $('#step21_ddlSeaPortLoading').val('0');
        $('#AddOtherLoading').html('');
    }
}

function UnLoadingData(num) 
{
    for (var i = 1; i <= 4; i++) {
        $('#btn_unloading_' + i).css('background-color', '#000');
        $('#btn_unloading_' + i).attr('selected', 'false');
    }
    $('#btn_unloading_' +(num + 1)).css('background-color', '#ccc');
    $('#btn_unloading_' + (num + 1)).attr('selected', 'true');

    if (typeof (Unloading_Arr[num]) != 'undefined') {
        $('#step21_ddlCountryUnloading').val(Unloading_Arr[num].country_id);
        $('#step21_ddlCountryUnloading').change();
        $('#step21_tbCityUnloading').val(Unloading_Arr[num].city);
        $('#step21_tbPostCodeUnloading').val(Unloading_Arr[num].postcode);
        $('#step21_tbDateUnloading').val(Unloading_Arr[num].date);
        $('#step21_tbTimeUnloading').val(Unloading_Arr[num].time);
        $('#step21_ddlAMPMUnloading').val(Unloading_Arr[num].ampm);
        $('#step21_ddlAirportUnloading').val(Unloading_Arr[num].airport_id);
        $('#step21_ddlSeaPortUnloading').val(Unloading_Arr[num].seaport_id);
        //Other Aiport
        if (typeof (Unloading_Arr[num].other_airport) != 'undefined') {
            if (Unloading_Arr[num].other_airport != '') 
            {
                var other_Airport = (Unloading_Arr[num].other_airport).split(' - ');
                $('#step21_ddlAirportUnloading').val('');
                LoadOtherStep2('', '', 'ddlAirportUnloading', false, other_Airport[0], other_Airport[1]);
                $('#AddOtherUnloading').show();
            }
        }
        //Other Seaport
        if (typeof (Unloading_Arr[num].other_seaport) != 'undefined') {
            if (Unloading_Arr[num].other_seaport != '') 
            {
                var other_SeaPort = (Unloading_Arr[num].other_seaport).split(' - ');
                $('#step21_ddlSeaPortUnloading').val('');
                LoadOtherStep2('', '', 'ddlSeaPortUnloading', false, other_SeaPort[0], other_SeaPort[1]);
                $('#AddOtherLoading').show();
            }
        }
    }
    else {
       
        $('#step21_ddlCountryUnloading').val('0');
        $('#step21_tbCityUnloading').val('');
        $('#step21_tbPostCodeUnloading').val('');
        $('#step21_tbDateUnloading').val('');
        $('#step21_tbTimeUnloading').val('0');
        $('#step21_ddlAMPMUnloading').val('0');
        $('#step21_ddlAirportUnloading').val('0');
        $('#step21_ddlSeaPortUnloading').val('0');
        $('#AddOtherUnloading').html('');
    }
}

function SaveLoading() {

    var selected = null;
    for (var i = 1; i <= 4; i++) {
        if ($('#btn_loading_' + i).attr('selected') == 'true') {
            selected = i;
        }
    }
    
    if (selected != null) {
        if (selected > Loading_Arr.length) {
            var Loading = {
                country_id: '',
                city: '',
                postcode: '',
                date: '',
                time: '',
                ampm: '',
                airport_id: '0',
                seaport_id: '0',
                other_airport: '',
                other_seaport: ''
            };

            Loading.country_id = $('#step21_ddlCountryLoading').val();
            Loading.city = $('#step21_tbCityLoading').val();
            Loading.postcode = $('#step21_tbPostCodeLoading').val();
            Loading.date = $('#step21_tbDateLoading').val();
            Loading.time = $('#step21_tbTimeLoading').val();
            Loading.ampm = $('#step21_ddlAMPMLoading').val();
            if (typeof ($('#step21_ddlAirportLoading').val()) != 'undefined') {
               
                if ($('#step21_ddlAirportLoading').val() == '') {
                    Loading.other_airport = $('#tb_PlaceName_step21_ddlAirportLoading').val() + ' - ' + $('#tb_PlaceShort_step21_ddlAirportLoading').val();
                }
                else {
                    Loading.airport_id = $('#step21_ddlAirportLoading').val(); 
                }
            }

            if (typeof ($('#step21_ddlSeaPortLoading').val()) != 'undefined') {

                if ($('#step21_ddlSeaPortLoading').val() == '') {
                    Loading.other_seaport = $('#tb_PlaceName_step21_ddlSeaPortLoading').val() + ' - ' + $('#tb_PlaceShort_step21_ddlSeaPortLoading').val();
                }
                else {
                    Loading.seaport_id = $('#step21_ddlSeaPortLoading').val();
                }
            }

            Loading_Arr.push(Loading);
        }
        else {

            if (typeof (Loading_Arr[selected - 1]) != 'undefined') {
                Loading_Arr[selected - 1].country_id = $('#step21_ddlCountryLoading').val();
                Loading_Arr[selected - 1].city = $('#step21_tbCityLoading').val();
                Loading_Arr[selected - 1].postcode = $('#step21_tbPostCodeLoading').val();
                Loading_Arr[selected - 1].date = $('#step21_tbDateLoading').val();
                Loading_Arr[selected - 1].time = $('#step21_tbTimeLoading').val();
                Loading_Arr[selected - 1].ampm = $('#step21_ddlAMPMLoading').val();
                Loading_Arr[selected - 1].airport_id = typeof($('#step21_ddlAirportLoading').val()!='undefined')?$('#step21_ddlAirportLoading').val():'0';
                Loading_Arr[selected - 1].seaport_id = typeof($('#step21_ddlSeaPortLoading').val()!='undefined')?$('#step21_ddlSeaPortLoading').val():'0';
            }
        }
    }
}

function DeleteLoading() {

    for (var i = Loading_Arr.length; i <= 4; i++) {
        if (i != 1) {
            $('#btn_loading_' + i).hide();
        }
    }
    $('#step21_btn_add_more_loading').show();
    
    Loading_Arr.pop();
    $('#step21_ddlCountryLoading').val('0');
    $('#step21_tbCityLoading').val('');
    $('#step21_tbPostCodeLoading').val('');
    $('#step21_tbDateLoading').val('');
    $('#step21_tbTimeLoading').val('0');
    $('#step21_ddlAMPMLoading').val('0');
    $('#step21_ddlAirportLoading').val('0');
    $('#step21_ddlSeaPortLoading').val('0');
    for (var i = 1; i <= 4; i++) {
        $('#btn_loading_' + i).css('background-color', '#000');
        $('#btn_loading_' + i).attr('selected', 'false');
    }
   
}

function AddMoreLoading() {

    if (Loading_Arr.length <= 4) {
        if (ValidateAddMore('Loading')) {
            
            SaveLoading();
            $('#AddOtherLoading').hide();
            $('#btn_loading_' + (Loading_Arr.length + 1)).show();

            if (Loading_Arr.length<4) {
                $('#step21_ddlCountryLoading').val('0');
                $('#step21_tbCityLoading').val('');
                $('#step21_tbPostCodeLoading').val('');
                $('#step21_tbDateLoading').val('');
                $('#step21_tbTimeLoading').val('0');
                $('#step21_ddlAMPMLoading').val('0');
                $('#step21_ddlAirportLoading').html('');
                $('#step21_ddlSeaPortLoading').html('');
            }

            for (var i = 1; i <= 4; i++) {
                $('#btn_loading_' + i).css('background-color', '#000');
                $('#btn_loading_' + i).attr('selected', 'false');
            }
            $('#btn_loading_' + (Loading_Arr.length + 1)).css('background-color', '#ccc');
            $('#btn_loading_' + (Loading_Arr.length + 1)).attr('selected', 'true');

            if (Loading_Arr.length == 4) {
                $('#step21_btn_add_more_loading').hide();
                $('#btn_loading_4').css('background-color', '#ccc');
                $('#btn_loading_4').attr('selected', 'true');
            }
        }
    }
}

function SaveUnLoading() {

    var selected = null;
    for (var i = 1; i <= 4; i++) {
        if ($('#btn_unloading_' + i).attr('selected') == 'true') {
            selected = i;
        }
    }

    if (selected != null) {
        if (selected > Unloading_Arr.length) {
            var Unloading = {
                country_id: '',
                city: '',
                postcode: '',
                date: '',
                time: '',
                ampm: '',
                airport_id: '0',
                seaport_id: '0',
                other_airport: '',
                other_seaport: ''
            };

            //Unloading data
            Unloading.country_id = $('#step21_ddlCountryUnloading').val();
            Unloading.city = $('#step21_tbCityUnloading').val();
            Unloading.postcode = $('#step21_tbPostCodeUnloading').val();
            Unloading.date = $('#step21_tbDateUnloading').val();
            Unloading.time = $('#step21_tbTimeUnloading').val();
            Unloading.ampm = $('#step21_ddlAMPMUnloading').val();
            if (typeof ($('#step21_ddlAirportUnloading').val()) != 'undefined') {

                if ($('#step21_ddlAirportUnloading').val() == '') {
                    Unloading.other_airport = $('#tb_PlaceName_step21_ddlAirportUnloading').val() + ' - ' + $('#tb_PlaceShort_step21_ddlAirportUnloading').val();
                } 
                else {
                Unloading.airport_id = $('#step21_ddlAirportUnloading').val();
                }
            }
            if (typeof ($('#step21_ddlSeaPortUnloading').val()) != 'undefined') {
                
                if ($('#step21_ddlSeaPortUnloading').val() == '') {
                    Unloading.other_seaport = $('#tb_PlaceName_step21_ddlSeaPortUnloading').val() + ' - ' + $('#tb_PlaceShort_step21_ddlSeaPortUnloading').val();
                } else {
                Unloading.seaport_id = $('#step21_ddlSeaPortUnloading').val();
                }
            }

            Unloading_Arr.push(Unloading);
        }
        else {
            if (typeof (Unloading_Arr[selected - 1]) != 'undefined') {
                Unloading_Arr[selected-1].country_id = $('#step21_ddlCountryUnloading').val();
                Unloading_Arr[selected-1].city = $('#step21_tbCityUnloading').val();
                Unloading_Arr[selected-1].postcode = $('#step21_tbPostCodeUnloading').val();
                Unloading_Arr[selected - 1].date = $('#step21_tbDateUnloading').val();
                Unloading_Arr[selected - 1].time = $('#step21_tbTimeUnloading').val();
                Unloading_Arr[selected - 1].ampm = $('#step21_ddlAMPMUnloading').val();
                Unloading_Arr[selected - 1].airport_id = typeof($('#step21_ddlAirportUnloading').val()!='undefined')?$('#step21_ddlAirportUnloading').val():'0';
                Unloading_Arr[selected - 1].seaport_id = typeof($('#step21_ddlSeaPortUnloading')!='undefined')?$('#step21_ddlSeaPortUnloading').val():'0';
            }
        }
    }
}

function DeleteUnLoading() {

    for (var i = Unloading_Arr.length; i <= 4; i++) {
        if (i != 1) {
            $('#btn_unloading_' + i).hide();
        }
    }
    $('#step21_btn_add_more_unloading').show();
    Unloading_Arr.pop();
    $('#step21_ddlCountryUnloading').val('0');
    $('#step21_tbCityUnloading').val('');
    $('#step21_tbPostCodeUnloading').val('');
    $('#step21_tbDateUnloading').val('');
    $('#step21_tbTimeUnloading').val('0');
    $('#step21_ddlAMPMUnloading').val('0');
    $('#step21_ddlAirportUnloading').val('0');
    $('#step21_ddlSeaPortUnloading').val('0');
    
    for (var i = 1; i <= 4; i++) {
        $('#btn_unloading_' + i).css('background-color', '#000');
        $('#btn_unloading_' + i).attr('selected', 'false');
    }
}

function AddMoreUnloading() {

    if (Unloading_Arr.length <= 4) {
        if (ValidateAddMore('Unloading')) {

            SaveUnLoading();
            $('#AddOtherUnloading').hide();
            $('#btn_unloading_' + (Unloading_Arr.length + 1)).show();

            if (Unloading_Arr.length < 4) {
                $('#step21_ddlCountryUnloading').val('0');
                $('#step21_tbCityUnloading').val('');
                $('#step21_tbPostCodeUnloading').val('');
                $('#step21_tbDateUnloading').val('');
                $('#step21_tbTimeUnloading').val('0');
                $('#step21_ddlAMPMUnloading').val('0');
                $('#step21_ddlAirportUnloading').html('');
                $('#step21_ddlSeaPortUnloading').html('');
            }
            
            for (var i = 1; i <= 4; i++) {
                $('#btn_unloading_' + i).css('background-color', '#000');
                $('#btn_unloading_' + i).attr('selected', 'false');
            }

            if (Unloading_Arr.length < 4) {
                $('#btn_unloading_' + (Unloading_Arr.length + 1)).css('background-color', '#ccc');
                $('#btn_unloading_' + (Unloading_Arr.length + 1)).attr('selected', 'true');
            }
            
            if (Unloading_Arr.length == 4) {
                $('#step21_btn_add_more_unloading').hide();
                $('#btn_unloading_4').css('background-color', '#ccc');
                $('#btn_unloading_4').attr('selected', 'true');
            }
        }
    }
}

//Loading Seaports or Airports
function loadLoadingPlaces(isloading,country_id) {

    var placetype_id = 0; //AirPorts
    var ddl_to_add = '';

    switch (Application.type_transport_id) {
        case '164': //Road Transport
        case '166': //Rail transport 
            return;
        break;
    case '40': //Combined transport 
        return;
        break;
        case '167'://Air Transport
            placetype_id = 3;
            if (isloading) {
                ddl_to_add = $('#step21_ddlAirportLoading');
            }
            else {
                ddl_to_add = $('#step21_ddlAirportUnloading');
            }
        break;
    case '165': //Sea transport
        placetype_id = 1;
        if (Application.type_offer_id == '69')//Cargo
        {
            if (isloading) {
                ddl_to_add = $('#step21_ddlSeaPortLoading');
            }
            else {
                ddl_to_add = $('#step21_ddlSeaPortUnloading');
            }
        }
        else//Transport
        {
            if (isloading) {
                ddl_to_add = $('#step21_ddlSeaPortLoading');
            }
            else {
                ddl_to_add = $('#step21_ddlSeaPortUnloading');
            }
        }
        break;
    }

    $.ajax
        ({
            url: "admin/webservice/Places.asmx/LoadPlacesByTypeAndCountry",
            data: '{placetype_id:' + placetype_id + ',country_id: ' + country_id + '}',
            dataType: "json",
            async: false,
            contentType: "application/json; charset=utf-8",
            type: "POST",
            complete: function(json1) {
                var c = JSON.parse(json1.responseText);
                var d = JSON.parse(c.d);
                ddl_to_add.html('')
                $('<option value="0"></option>').appendTo(ddl_to_add);
                var record_count = 0;
                for (var i in d) {
                    record_count++;
                    $('<option value="' + i + '">' + d[i] + '</option>').appendTo(ddl_to_add);
                }
                $('<option value="">Other</option>').appendTo(ddl_to_add);
                
            }
        });
}



//function for seting size on the modal
function AppModalShow(dialog)
{
    dialog.container.css({
        width: 737
    });
    LoadAppStep(1);
}

function AppModalOpen(dialog) {

    dialog.overlay.fadeIn('fast', function() {
        dialog.container.fadeIn('fast', function() {
            dialog.data.hide().slideDown('fast');

        });
    });
}

function closeModal() {
    $.modal.close();
    window.location.href = window.location.href;
}


function ValidateApp(validate){
    var returnVal = true;
    if (validate) {
        $("#formDiv input[validate='true']").each(function(val, i) {
            $("#" + i.id + "_rfv").css('color', '#000000');
            if ($("#" + i.id).val() == "") {
                returnVal = false;
                $("#span" + i.id).remove();
                $("#" + i.id + "_rfv").attr('style', 'color:red;font-weight:bold');
                $("#" + i.id).css('border-style', 'solid 1px');
                $("#" + i.id).css('border-color', 'red');
                $("#" + i.id).blur(function() {
                    if ($(this).val() != "") {
                        $("#" + i.id).css('border-style', 'solid 1px');
                        $("#" + i.id).css('border-color', 'black');
                        $("#" + i.id + "_rfv").css('color', '#000000');
                    }
                });
            }
        });

        $("#formDiv textarea").each(function(val, i) {
            $("#" + i.id + "_rfv").css('color', '#000000');
            if ($("#" + i.id).val().length > 199) {
                returnVal = false;
                $("#span" + i.id).remove();
                $("#" + i.id + "_rfv").attr('style', 'color:red;font-weight:bold');
                $("#" + i.id).css('border-style', 'solid 1px');
                $("#" + i.id).css('border-color', 'red');
                $("#" + i.id).blur(function() {
                    if ($(this).val() != "") {
                        $("#" + i.id).css('border-style', 'solid 1px');
                        $("#" + i.id).css('border-color', 'black');
                        $("#" + i.id + "_rfv").css('color', '#000000');
                    }
                });
            }
        });
        $("#formDiv select[validate='true']").each(function(val, i) {
            $("#" + i.id + "_rfv").css('color', '#000000');
            if ($("#" + i.id).val() == 0) {
                returnVal = false;
                $("#span" + i.id).remove();
                $("#" + i.id + "_rfv").attr('style', 'color:red;font-weight:bold');
                $("#" + i.id).css('border-style', 'solid 1px');
                $("#" + i.id).css('border-color', 'red');
                $("#" + i.id).blur(function() {
                    if ($(this).val() != "") {
                        $("#" + i.id).css('border-style', 'solid 1px');
                        $("#" + i.id).css('border-color', 'black');
                        $("#" + i.id + "_rfv").css('color', '#000000');
                    }
                });
            }
        });
    }
    return returnVal;
}

function SaveApplication() {
    if (ValidateApp(true))
    {
        var step3_data = null;
        switch (Application.type_transport_id)
         {
             case '167': //Airway
                 //Step3Air.airport_loading_id = $('#step31_ddlAirportLoading').val();
                 //Step3Air.airport_unloading_id = $('#step31_ddlAirportUnloading').val();
                 Step3Air.level_importance_id = $('#step31_ddlLevelOfImportnce').val();
                 Step3Air.universal_type_id = $('#step31_ddlCraftTypes').val();
                 Step3Air.placement_type_id = $('#step31_ddlPlacementTypes').val();
                 Step3Air.packing_type_id = $('#step31_ddlPackingTypes').val();
                 Step3Air.length = ($('#step31_tbLenght').val()!='')?$('#step31_tbLenght').val() +' '+ $('#step31_ddlLenght').val():'';
                 Step3Air.width = ($('#step31_tbWidth').val()!='')?$('#step31_tbWidth').val() + ' ' + $('#step31_ddlWidth').val():'';
                 Step3Air.height = ($('#step31_tbHeight').val()!='')?$('#step31_tbHeight').val() + ' ' + $('#step31_ddlHeight').val():'';
                 Step3Air.weight = ($('#step31_tbWeight').val()!='')?$('#step31_tbWeight').val() + ' ' + $('#step31_ddlWeight').val():'';
                 Step3Air.volume = ($('#step31_tbVolume').val()!='')?$('#step31_tbVolume').val() + ' ' + $('#step31_ddlVolume').val():'';
                 Step3Air.price = ($('#step31_tbPrice').val()!='')?$('#step31_tbPrice').val() + ' ' + $('#step31_ddlPrice').val():'';
                 Step3Air.special_conditions_id = $('#step31_ddlSpecualConditionTypes').val();
                 Step3Air.other_information = $('#step31_tbOtherInformattions').val();
                 Step3Air.type = 1;
                 step3_data = Step3Air;
           break;
           case '166': //Railway
           case '164': //Road
               Step3Transport.level_importance_id = $('#step3_road_ddlLevelOfImportnce').val();
               Step3Transport.vehicle_cat = $('#step3_road_ddl_vehicle_cat').val();
               Step3Transport.universal_type_id = $('#step3_road_ddlVehicleTypes').val();
               Step3Transport.placement_type_id = $('#step3_road_ddlPlacementTypes').val();
               Step3Transport.packing_type_id = $('#step3_road_ddlPackingTypes').val();
               Step3Transport.length = ($('#step3_road_tbLenght').val() != '') ? $('#step3_road_tbLenght').val() + ' ' + $('#step3_road_ddlLenght').val() : '';
               Step3Transport.width = ($('#step3_road_tbWidth').val() != '') ? $('#step3_road_tbWidth').val() + ' ' + $('#step3_road_ddlWidth').val() : '';
               Step3Transport.height = ($('#step3_road_tbHeight').val() != '') ? $('#step3_road_tbHeight').val() + ' ' + $('#step3_road_ddlHeight').val() : '';
               Step3Transport.weight = ($('#step3_road_tbWeight').val() != '') ? $('#step3_road_tbWeight').val() + ' ' + $('#step3_road_ddlWeight').val() : '';
               Step3Transport.volume = ($('#step3_road_tbVolume').val() != '') ? $('#step3_road_tbVolume').val() + ' ' + $('#step3_road_ddlVolume').val() : '';
               Step3Transport.price = ($('#step3_road_tbPrice').val() != '') ? $('#step3_road_tbPrice').val() + ' ' + $('#step3_road_ddlPrice').val() : '';
               Step3Transport.special_conditions_id = $('#step3_road_ddlSpecualConditionTypes').val();
               Step3Transport.other_information = $('#step3_road_tbOtherInformattions').val();
               step3_data = Step3Transport;
               break;
           case '165': //Seaway
               if (Application.type_offer_id == '69')//Cargo
               {
                   //Step3SeaCargo.seaport_loading_id = $('#step3_sea_cargo_ddlSeaPortLoading').val();
                   //Step3SeaCargo.seaport_unloading_id = $('#step3_sea_cargo_ddlSeaPortUnloading').val();
                   Step3SeaCargo.level_importance_id = $('#step3_sea_cargo_ddlLevelOfImportnce').val();
                   Step3SeaCargo.universal_type_id = $('#step3_sea_cargo_ddlVeselTypes').val();
                   Step3SeaCargo.placement_type_id = $('#step3_sea_cargo_ddlPlacementTypes').val();
                   Step3SeaCargo.packing_type_id = $('#step3_sea_cargo_ddlPackingTypes').val();
                   Step3SeaCargo.length = ($('#step3_sea_cargo_tbLenght').val() != '') ? $('#step3_sea_cargo_tbLenght').val() + ' ' + $('#step3_sea_cargo_ddlLenght').val() : '';
                   Step3SeaCargo.width = ($('#step3_sea_cargo_tbWidth').val()!='')?$('#step3_sea_cargo_tbWidth').val() + ' ' + $('#step3_sea_cargo_ddlWidth').val():'';
                   Step3SeaCargo.height = ($('#step3_sea_cargo_tbHeight').val()!='')?$('#step3_sea_cargo_tbHeight').val() + ' ' + $('#step3_sea_cargo_ddlHeight').val():'';
                   Step3SeaCargo.weight = ($('#step3_sea_cargo_tbWeight').val()!='')?$('#step3_sea_cargo_tbWeight').val() + ' ' + $('#step3_sea_cargo_ddlWeight').val():'';
                   Step3SeaCargo.volume = ($('#step3_sea_cargo_tbVolume').val()!='')?$('#step3_sea_cargo_tbVolume').val() + ' ' + $('#step3_sea_cargo_ddlVolume').val():'';
                   Step3SeaCargo.price = ($('#step3_sea_cargo_tbPrice').val()!='')?$('#step3_sea_cargo_tbPrice').val() + ' ' + $('#step3_sea_cargo_ddlPrice').val():'';
                   Step3SeaCargo.special_conditions_id = $('#step3_sea_cargo_ddlSpecualConditionTypes').val();
                   Step3SeaCargo.other_information = $('#step3_sea_cargo_tbOtherInformattions').val();
                   step3_data = Step3SeaCargo;
               }
               else//Transport
               {
                   Step3SeaTransport.level_importance_id = $('#step3_sea_transport_ddlLevelOfImportnce').val();
                   Step3SeaTransport.placement_type_id = $('#step3_sea_transport_ddlPlacementTypes').val();
                   Step3SeaTransport.country_id = $('#step3_sea_transport_ddlDeparture').val();
                   Step3SeaTransport.seaport_id = $('#step3_sea_transport_ddlSeaPortLoading').val();
                   Step3SeaTransport.universal_type_id = $('#step3_sea_transport_ddlVeselTypes').val();
                   Step3SeaTransport.vessel_build_date = $('#step3_sea_transport_tbVesselBuiltDate').val();
                   Step3SeaTransport.flag_country_id = $('#step3_sea_transport_ddlFlag').val();
                   Step3SeaTransport.clas = $('#step3_sea_transport_tbClass').val();
                   Step3SeaTransport.date_duration = $('#step3_sea_transport_tbDateOfDuration').val();
                   Step3SeaTransport.time_duration = ($('#step3_sea_transport_tbTimeOfDuration').val()!='')?$('#step3_sea_transport_tbTimeOfDuration').val() +' '+ $('#step3_sea_transport_ddl_time_period').val():'';
                   Step3SeaTransport.price = ($('#step3_sea_transport_tbPrice').val()!='')?$('#step3_sea_transport_tbPrice').val() +' '+  $('#step3_sea_transport_ddlPrice').val():'';
                   Step3SeaTransport.special_conditions_id = $('#step3_sea_transport_ddlSpecualConditionTypes').val();
                   Step3SeaTransport.other_information = $('#step3_sea_transport_tbOtherInformattions').val();
                   Step3SeaTransport.route = $('#step3_sea_transport_ddlRute').val();
                   step3_data = Step3SeaTransport;
               }  
           break;
         }
         if (Application.type_offer_id == '71')//TenderApp
         {
             Step3Tender.placement_type_id = $('#step21_ddlPlacementTypes').val();
             Step3Tender.transport_type_id = $('#step21_ddlTransportTypes').val();
             Step3Tender.packing_type_id = $('#step21_ddlPackingTypes').val();
             Step3Tender.type_goods_id = $('#step21_ddlGoods').val();
             Step3Tender.weight_goods = ($('#step21_tbWeightOfGoods').val()!='')?$('#step21_tbWeightOfGoods').val() + " " + $('#step21_ddlWeight').val():'';
             Step3Tender.payment_type_id = $('#step21_tbPaymentType').val();
             Step3Tender.special_condition_type = $('#step21_ddlSprecialCondition').val();
             Step3Tender.other_information = $('#step21_tbOtherInformattions').val();
             step3_data = Step3Tender;
         }

        $("#formDiv").html('');
        $("#formDiv").html(loaderDiv);

        $.ajax
        ({
        url: "webservices/Application.asmx/SaveApplication",
        data: '{application: ' + $.toJSON(Application) +
              ',loading:' + $.toJSON(Loading_Arr) +
              ',unloading:' + $.toJSON(Unloading_Arr) +
              ',step2:' + $.toJSON(Step2CargoTransport) +
              ',step3:' + $.toJSON(step3_data) + '}',
            dataType: "json",
            async: false,
            contentType: "application/json; charset=utf-8",
            type: "POST",
            complete: function(json1) {
                var c = JSON.parse(json1.responseText);
                var d = JSON.parse(c.d);
                alert(d.message)
                closeModal();
            }
        });
         
     }
     return false;
}

function LoadAppStep(step,validate) {
    switch (step) 
    {
        case 1:
            $("#formDiv").html('');
            $("#formDiv").html(loaderDiv);
            $("#formDiv").load("application/step1.aspx", {},
            function() {
                if (status == 'error') {
                    alert('there was an error in file loading');
                }
                if ($('#step11_tb_comany_id').val() == "") {
                    alert($('#step11_login_info').text());
                    $.modal.close();
                }
                else {
                    Application.company_id = $('#step11_tb_comany_id').val();
                    $('#step11_ddlOffer').val(Application.type_offer_id);
                    $('#step11_ddlTransport').val(Application.type_transport_id);

                }
                $("#loader").remove();
            });
            break;
        case 2:
            if (ValidateApp(validate)) {

                if (validate) {
                    Application.type_offer = $('#step11_ddlOffer option:selected').text();
                    Application.type_transport = $('#step11_ddlTransport option:selected').text();
                    Application.type_offer_id = $('#step11_ddlOffer').val();
                    Application.type_transport_id = $('#step11_ddlTransport').val();
                }

                var file_for_load = 'step2.aspx?type_offer_id=' + Application.type_offer_id + '&type_transport_id=' + Application.type_transport_id;

                //Generate text header
                var header_text = new Array();
                header_text.push('-');
                header_text.push(Application.type_offer);
                header_text.push(Application.type_transport);

                $("#formDiv").html('');
                $("#formDiv").html(loaderDiv);
                $("#formDiv").load("application/" + file_for_load, {},
                    function() {
                        if (status == 'error') {
                            alert('there was an error in file loading');
                        }
                        $('#form_title').append(header_text.join(' '));
                        $("#loader").remove();
                        if (Loading_Arr.length > 0) {
                            //Loading data                        
                            $('#step21_ddlCountryLoading').val(Loading_Arr[0].country_id);
                            $('#step21_ddlCountryLoading').change();
                            $('#step21_tbCityLoading').val(Loading_Arr[0].city);
                            $('#step21_tbPostCodeLoading').val(Loading_Arr[0].postcode);
                            $('#step21_tbDateLoading').val(Loading_Arr[0].date);
                            $('#step21_tbTimeLoading').val(Loading_Arr[0].time);
                            $('#step21_ddlAMPMLoading').val(Loading_Arr[0].ampm);
                            $('#step21_ddlAirportLoading').val(Loading_Arr[0].airport_id);
                            $('#step21_ddlSeaPortLoading').val(Loading_Arr[0].seaport_id);

                            //$('#btn_loading_1').attr('selected', true);
                            //$('#btn_loading_1').css('background-color', '#CCCCCC');
                        }

                        //Unloading data
                        if (Unloading_Arr.length > 0) {
                            $('#step21_ddlCountryUnloading').val(Unloading_Arr[0].country_id);
                            $('#step21_ddlCountryUnloading').change();
                            $('#step21_tbCityUnloading').val(Unloading_Arr[0].city);
                            $('#step21_tbPostCodeUnloading').val(Unloading_Arr[0].postcode);
                            $('#step21_tbDateUnloading').val(Unloading_Arr[0].date);
                            $('#step21_tbTimeUnloading').val(Unloading_Arr[0].time);
                            $('#step21_ddlAMPMUnloading').val(Unloading_Arr[0].ampm);
                            $('#step21_ddlAirportUnloading').val(Unloading_Arr[0].airport_id);
                            $('#step21_ddlSeaPortUnloading').val(Unloading_Arr[0].seaport_id);

                            //$('#btn_unloading_1').attr('selected', true);
                            //$('#btn_unloading_1').css('background-color', '#CCCCCC');
                        }
                    });
            }
            break;
        case 3:
            if (ValidateApp(true)) {

                //Loading data
                SaveLoading();
                //Unloading data
                SaveUnLoading();

                var country_load_id = 0;
                var country_unload_id = 0;

                if (typeof (Loading_Arr[0]) != 'undefined') {
                    country_load_id = Loading_Arr[0].country_id;
                }

                if (typeof (Unloading_Arr[0]) != 'undefined') {
                    country_unload_id = Unloading_Arr[0].country_id;

                }

                if (Application.type_offer_id == '71') {
                    Step3Tender.opening_date = $('#step21_tbDateOpening').val();
                    Step3Tender.closing_date = $('#step21_tbDateClosing').val();
                }

                var file_for_load = 'step3_air.aspx';
                var type_transport = 14;
                switch (Application.type_transport_id) {
                    case '164': //Road Transport
                        file_for_load = 'step3_road.aspx?typeid=14';
                        type_transport = 14;
                        break;
                    case '166': //Rail transport
                        file_for_load = 'step3_road.aspx?typeid=13';
                        type_transport = 13;
                        break;
                    case '40': //Combined transport 
                        file_for_load = 'step3_combined_cargo.aspx';
                        break;
                    case '167': //Air Transport
                        file_for_load = 'step3_air.aspx?country_load_id=' + country_load_id + '&country_unload_id=' + country_unload_id + "&type_offer_id=" + Application.type_offer_id;
                        type_transport = 1;
                        break;
                    case '165': //Sea transport 
                        if (Application.type_offer_id == '69')//Cargo
                        {
                            file_for_load = 'step3_sea_cargo.aspx?country_load_id=' + country_load_id + '&country_unload_id=' + country_unload_id;
                        }
                        else//Transport
                        {
                            file_for_load = 'step3_sea_transport.aspx?country_load_id=' + country_load_id + '&country_unload_id=' + country_unload_id;
                        }
                        type_transport = 3;
                        break;
                }
                if (Application.type_offer_id == '71') {
                    file_for_load = 'step3_tender.aspx?typeid=' + type_transport;
                }

                //Generate text header
                var header_text = new Array();
                header_text.push('-');
                header_text.push(Application.type_transport);
                header_text.push(Application.type_offer);

                $("#formDiv").html('');
                $("#formDiv").html(loaderDiv);
                $("#formDiv").load("application/" + file_for_load, {},
                    function() {
                        if (status == 'error') {
                            alert('there was an error in file loading');
                        }
                        $('#form_title').append(header_text.join(' '));
                        $("#loader").remove();
                    });
            }
            break;
        default: 
        break;
    }
}

